package mage.factory.handler;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

public abstract class DefaultHandler extends org.xml.sax.helpers.DefaultHandler{
	private static final Logger log = LogManager.getLogger(DefaultHandler.class);
	public DefaultHandler() {
		this.elements=new ArrayList<DefaultHandlerElement>();
	}

	public Logger file_logger = null;
	protected Logger getLogger(){
		if(this.file_logger==null){
			this.file_logger = LogManager.getLogger("logfile");
		}
		return this.file_logger;
	}
	protected String element = "";
	protected boolean current_element_valid = true;
	protected String getCurrentElement(){
		return this.element;
	}
	public boolean isElementAlreadyExisting(String name,String setpackage){
		String normalized_name = name.replace(" ","").replace("Edition","");
		try {
			Class.forName(setpackage+normalized_name);
		} catch (ClassNotFoundException e) {
			return false;
		}
		return true;
	}
	
	protected boolean element_is_start = false;
	private String temp_char = "";
	public abstract void startElement(String qName,Attributes attributes) throws SAXException ;
	public void startElement(String uri, String localName, String qName,Attributes attributes) throws SAXException {
		//log.info("Start Element :" + qName);
		this.element=qName;
		this.element_is_start=true;
		this.startElement(qName, attributes);
	}
	public abstract void endElement(String qName,String characters) throws SAXException;
	public void endElement(String uri, String localName, String qName) throws SAXException {
		//log.info("End Element :" + qName);
		this.element_is_start=false;
		this.endElement(qName,this.temp_char);
		this.temp_char="";
	}
	public abstract boolean characters(String characters) throws SAXException;
	public void characters(char ch[], int start, int length) throws SAXException {
		//log.info(new String(ch,start,length));
		String characters = new String(ch,start,length);
		if(this.element_is_start){
			characters=characters.replaceAll("\n","").trim();
			if (characters.indexOf("&apos;")>0){
				characters=characters.replaceAll("&apos;","'");
			}
			if (characters.indexOf("&quot;")>0){
				characters=characters.replaceAll("&quot;","\"");
			}
			if (characters.indexOf(";")>0){
				characters=characters.replaceAll(";",",");
			}
			temp_char+=characters;
			if(!this.characters(temp_char)){
				//log.info(this.element+"="+new String(ch,start,length));
			}
		}
	}
	
	protected List<DefaultHandlerElement> elements = null;
	public List<DefaultHandlerElement> getElements(){
		return this.elements;
	}
	public String toString(){
		StringBuffer sb = new StringBuffer();
		for (DefaultHandlerElement se : this.elements) {
			sb.append(se.toClass());
		}
		return sb.toString();
	}
	public void toFile(String basepath){
		File f = new File(basepath);
		f.mkdir();
		
		for (DefaultHandlerElement se : this.elements) {
			if(se.getPackageName()!=null){
				f=new File(basepath+"\\"+se.getPackageName());
				f.mkdir();
			}
			try {
				BufferedWriter out = new BufferedWriter(new FileWriter(
							new File(
									basepath
									+"\\"
									+se.getClassName()
									+".java"
									)
						));
				out.write(se.toClass());
				out.close();
			} catch (IOException e) {
				log.error(se.getPackageName());
				log.error(e);
			}
		
		}
	}
	public void toFile(String basepath,String filter){
		File f = new File(basepath);
		f.mkdir();
		
		for (DefaultHandlerElement se : this.elements) {
			if(filter.equalsIgnoreCase(se.getClassName())){
				if(se.getPackageName()!=null){
					f=new File(basepath+"\\"+se.getPackageName());
					f.mkdir();
				}
				try {
					BufferedWriter out = new BufferedWriter(new FileWriter(
								new File(
										basepath
										+"\\"
										+se.getClassName()
										+".java"
										)
							));
					out.write(se.toClass());
					out.close();
				} catch (IOException e) {
					log.error(se.getPackageName());
					log.error(e);
				}
			}
		}
	}
}

interface DefaultHandlerElement{
	public String toClass();
	public String getPackageName();
	public String getClassName();
}